package com.efuture.androidmvvmdemo.view.ui

import android.os.Bundle
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.efuture.androidmvvmdemo.R
import com.efuture.androidmvvmdemo.base.BaseActivity
import com.efuture.androidmvvmdemo.databinding.ActivityMainBinding
import com.efuture.androidmvvmdemo.listener.MainListener
import com.efuture.androidmvvmdemo.view.adapter.HomeDataAdapter
import com.efuture.androidmvvmdemo.viewmodel.MainViewModel
import com.efuture.netrequestdemo.utils.AppGlobal

class MainActivity : BaseActivity() {
    private lateinit var binding: ActivityMainBinding
    private lateinit var mainViewModel:MainViewModel

    private lateinit var mainListener: MainListener
    private lateinit var homeDataAdapter:HomeDataAdapter
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportActionBar?.hide()
        binding = DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main)
        mainViewModel = ViewModelProvider(this).get(MainViewModel::class.java)
        mainListener = MainListener(mainViewModel,this)

        observeData()
        getHomeData()

    }

    private fun getHomeData() {
        mainViewModel.userData.value = AppGlobal.userData
        homeDataAdapter = HomeDataAdapter(null)
        binding?.rvHomeData.layoutManager = LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false)
        binding?.rvHomeData?.adapter  = homeDataAdapter
        val cid = "294"//测试数据
        mainListener.getHomeData(cid)
    }

    private fun observeData() {
        mainViewModel.userData.observe(this,{
            binding.tvUser.text = "用户:"+it.data.nickname
        })
        mainViewModel.homeData.observe(this,{
            mainListener.dealWithHomeDataResult(it,homeDataAdapter)
        })
    }
}